//
//  CJNavigationController.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/12.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit

class CJNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 隐藏系统导航栏
        navigationBar.isHidden = true
    }
}

// MARK: - 控制器push及返回方法
extension CJNavigationController {
    
    /// 重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            // 非rootController时隐藏tabbar
            viewController.hidesBottomBarWhenPushed = true;
            // 控制器多级push时, leftbaritem显示 返回文字
            if let VC = viewController as? CJBaseViewController {
                var letBarTitle = "返回"
                
                if viewControllers.count  == 1 {
                    letBarTitle = viewControllers.first?.title ?? "返回"
                }
                VC.navItem.leftBarButtonItem = UIBarButtonItem(title: letBarTitle, target: self, action: #selector(popToParentVC), isBack: true)
            }
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    /// 返回方法
    @objc private func popToParentVC() {
        popViewController(animated: true)
    }
}
